/*
 * power.h
 *
 * power management driver.
 *
 * Copyright (C) 2022 Microwell, Inc.
 * Subject to the GNU Public License, version 2.
 *
 * Author: guoming<guoming@microwell.net>
 */
#ifndef _POWER_SUPPLY_H_
#define _POWER_SUPPLY_H_

#include "mcu_regs.h"
#include "types.h"

#define CPU_IDLE()                          (SET_BIT(PCON, IDLE))
#define CPU_STOP()                          do { SET_BIT(PCON, STOP); CLEAR_BIT(PCON, STOP); } while (0)     

/*=============================================================================
 *                              Power config
 *===========================================================================*/
/* If enable DCIN(vin or vkey) over voltage detect */
#define CONFIG_POWER_USING_OVD      (0)

/* DCIN wakeup config:
 * 0, disable; 1, DCIN plugin or plugout wakeup, 2, DCIN in plugin status wakeup */
#define CONFIG_POWER_USING_DCIN_WK  (2)
/* 0, disable; 1, enable */
#define CONFIG_POWER_USING_WDOG_WK  (0)

/* I/O port wakeup:
 * 0, disable; 1, low level; 2, high level; 3, fall edge; 4, rise edge;
 * 5, high pulse; 6, low pulse
 * 7, based on I/O current level and set to the oppsite automaticly.
 * NOTE: P03/P04 support low level and high level wakeup
 *       P06 support low level/high level/fall edge/rise edge wakeup
 *       P07/P11 support all types
 */
#define CONFIG_POWER_USING_P03_WK   (0)
#define CONFIG_POWER_USING_P04_WK   (1)
#define CONFIG_POWER_USING_P06_WK   (0)
#define CONFIG_POWER_USING_P07_WK   (7)
#define CONFIG_POWER_USING_P11_WK   (0)
#define CONFIG_POWER_USING_P20_WK   (0)   /*** TODO: P07 remap P20    **/
#define CONFIG_POWER_USING_P22_WK   (0)  
/*=============================================================================
 *                            Power config end
 *===========================================================================*/
#define POWER_IS_DCIN_OK()           (!!READ_BIT(PWRSTAT, DCINOK))
#define POWER_IS_VIN_OK()            (!!READ_BIT(PWRSTAT, VINOK))
#define POWER_IS_VKEY_OK()           (POWER_IS_DCIN_OK() && !POWER_IS_VIN_OK())

void power_init(void);
void power_exit(void);

void power_prepare_stop(void);
/*
 * Put system to STOP mode, after wakeup,
 * the wakeup reason(a bitmask) will return.
 */
uint8_t power_enter_stop(void);

#endif
